package com.softer.wxzj.controller;

import com.softer.wxzj.common.aop.annotation.LogRecord;
import com.softer.wxzj.common.aop.enums.LogOperationType;
import com.softer.wxzj.common.aop.enums.LogOperationUnit;
import com.softer.wxzj.common.entity.CommonResult;
import com.softer.wxzj.entity.*;
import com.softer.wxzj.service.FileService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;


/**
 * 附件控制接口
 *
 * @author Gong Zhiyang
 * @date 2019/4/30
 */
@RestController
@RequestMapping("/file")
@Slf4j
public class FileController {
    @Autowired
    private FileService fileService;

    /**
     * 上传附件（不存库）
     *
     * @param file       附件
     * @param fileUpload 上传信息
     * @return 结果
     */
    @PostMapping("/upload")
    public CommonResult upload(@RequestParam MultipartFile file, FileUpload fileUpload) {
        return fileService.upload(file, fileUpload);
    }

    /**
     * 上传附件保存到数据库
     *
     * @param list 附件集合
     * @return 结果
     */
    @PostMapping("/saveFile")
    public CommonResult saveFile(@RequestBody List<FileForDialog> list) {
        return fileService.saveFile(list);
    }

    /**
     * 删除附件
     *
     * @param id 附件ID
     * @return 删除结果
     */
    @PostMapping("/delete")
    @LogRecord(description = "删除附件", operationUnit = LogOperationUnit.FILE, operationType = LogOperationType.DELETE)
    public CommonResult delete(String id) {
        return fileService.delete(id);
    }


    /**
     * 通过文件类型查询文件
     *
     * @param fileUpload 查询实体
     * @return 文件信息
     */
    @PostMapping("/getFileList")
    @LogRecord(description = "查询附件列表", operationUnit = LogOperationUnit.FILE, operationType = LogOperationType.SELECT)
    public List<FileUpload> getFileList(FileUpload fileUpload) {
        return fileService.getFileList(fileUpload);
    }

    /**
     * 获取业务ID的文件类型已经数量
     * （只查询图档类型是正常状态的）
     *
     * @param id      业务id
     * @param arcType 附件大类(以逗号分隔)
     * @return 文件类型和数量
     */
    @PostMapping("/getFileType")
    @LogRecord(description = "查询文件类型和数量", operationUnit = LogOperationUnit.FILE, operationType = LogOperationType.SELECT)
    public List<FileType> getFileType(String id, String arcType) {
        return fileService.getFileType(id, arcType);
    }

    @PostMapping("/getBusiFileList")
    @LogRecord(description = "查询文件类型和数量", operationUnit = LogOperationUnit.FILE, operationType = LogOperationType.SELECT)
    public List<BaseOwnerInfo> getBusiFileList(String busiNo, String arcType) {
        return fileService.getBusiFileList(busiNo, arcType);
    }

    /**
     * 获取业务ID的文件类型已经数量
     *
     * @param id      业务id
     * @param arcType 附件大类(以逗号分隔)
     * @return 文件类型和数量
     */
    @PostMapping("/getFileVOList")
    @LogRecord(description = "查询文件类型和数量", operationUnit = LogOperationUnit.FILE, operationType = LogOperationType.SELECT)
    public List<FileVO> getFileVOList(String id, String arcType) {
        return fileService.getFileVOList(id, arcType);
    }

    /**
     * 校验图档上传的必传项
     *
     * @param businessId 业务ID
     * @param type       图档类型（用逗号分隔）
     * @return 校验结果
     */
    @PostMapping("/checkFile")
    @LogRecord(description = "校验图档上传的必传项", operationUnit = LogOperationUnit.FILE, operationType = LogOperationType.SELECT)
    public CommonResult checkFile(String businessId, String type) {
        return fileService.checkFile(businessId, type);
    }

    /**
     * 获取变更的图档详情
     *
     * @param modifyDetailId 变更项ID
     * @return 变更图档详情
     */
    @PostMapping("/getModifyFileDetail")
    @LogRecord(description = "获取变更的图档详情", operationUnit = LogOperationUnit.FILE, operationType = LogOperationType.SELECT)
    public FileDetailList getModifyFileDetail(@RequestParam("modifyDetailId") String modifyDetailId) {
        return fileService.getModifyFileDetail(modifyDetailId);
    }
    /**
     * 获取图片路径
     *
     * @param id       业务ID
     * @param type     大类
     * @param fileType 小类
     * @return 路径集合
     */
    @PostMapping("/getFilePathListByFileUpload")
    @LogRecord(description = "获取图片路径", operationUnit = LogOperationUnit.FILE, operationType = LogOperationType.SELECT)
    public List<String> getFilePathListByFileUpload(String id, String type, String fileType){
        return fileService.getFilePathListByFileUpload(id, null, type, fileType);
    }
}
